/*
 * Copyright 2020-2025 the original author or authors.
 * You cannot use this file unless authorized by the author.
 */

package org.ipig.commons.helper;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * ImageHelper
 *
 * @author <a href="mailto:comchnts@163.com">chinats</a>
 * @version $$Id: ImageHelper.java 3 2020-01-21 09:36:33Z ts <comchnts@163.com> $$
 * @since 1.0
 */
public class ImageHelper {

    /**
     * drawMsgOnImage
     *
     * @param origImage  BufferedImage
     * @param rectConf   DrawRectConf
     * @param strConf    DrawMsgConf
     * @param outPutPath String
     * @return
     */
    public static boolean drawMsgOnImage(BufferedImage origImage, DrawRectParm rectConf, DrawMsgParm strConf, String outPutPath) {
        boolean flag = false;
        try {
            // 1）.获取图片的原始宽高
            int height = origImage.getHeight();
            int width = origImage.getWidth();

            Graphics2D g = origImage.createGraphics();
            //2）画一个着色块,用来覆盖背景中的内容
            g.setColor(Color.white);//设置画笔的颜色
            g.fill3DRect(rectConf.getX(), rectConf.getY(), rectConf.getWidth(), rectConf.getHeight(), rectConf.isRaised());//画填充矩形

            //3）在背景图片某个位置写入文字信息,利用Graphics2D工具绘制，将文字写入到图片当中
            g.setColor(strConf.getColor());//设置画笔的颜色
            g.setFont(strConf.getFont());//设置画笔的字体
            g.drawString(strConf.getMsg(), strConf.getX(), strConf.getY());//画字符串
            g.dispose();

            //4）输出图片信息
            flag = ImageIO.write(origImage, "jpg", new File(outPutPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * buildDrawRectConf
     *
     * @param x
     * @param y
     * @param height
     * @param width
     * @param raised
     * @return
     */
    public static DrawRectParm buildDrawRectConf(int x, int y, int height, int width, boolean raised) {
        DrawRectParm conf = new DrawRectParm();
        conf.setX(x);
        conf.setY(y);
        conf.setHeight(height);
        conf.setWidth(width);
        conf.setRaised(raised);
        return conf;
    }

    /**
     * buildDrawMsgConf
     *
     * @param x
     * @param y
     * @param color
     * @param font
     * @param msg
     * @return
     */
    public static DrawMsgParm buildDrawMsgConf(int x, int y, Color color, Font font, String msg) {
        DrawMsgParm conf = new DrawMsgParm();
        conf.setColor(Color.RED);
        conf.setFont(font);
        conf.setX(x);
        conf.setY(y);
        conf.setMsg(msg);
        return conf;
    }
}

@Data
@NoArgsConstructor
class DrawRectParm {
    private int x;
    private int y;
    private int width;
    private int height;
    private boolean raised = true;
}

@Data
@NoArgsConstructor
class DrawMsgParm {
    private int x;
    private int y;
    private Color color;
    private Font font;
    private String msg = "";
}
